package tau.pimpmyfreedb.common.types;

import java.util.ArrayList;
import java.util.List;

public class EditData {

    MusicItem musicItemToUpdate;

    public MusicItem getMusicItemToUpdate() {
	return musicItemToUpdate;
    }

    public void setMusicItemToUpdate(MusicItem musicItemToUpdate) {
	this.musicItemToUpdate = musicItemToUpdate;
    }

    ArrayList<MusicItem> itemsToCreate, itemsToDelete;
    ArrayList<Tuple<MusicItem, MusicItem>> relations, oldRelations;

    public EditData() {
	itemsToCreate = new ArrayList<MusicItem>();
	itemsToDelete = new ArrayList<MusicItem>();
	relations = new ArrayList<Tuple<MusicItem, MusicItem>>();
	oldRelations = new ArrayList<Tuple<MusicItem, MusicItem>>();
	musicItemToUpdate = null;
    }

    public void addItemToCreate(MusicItem item) {
	itemsToCreate.add(item);
    }

    public void addItemtoRemove(MusicItem item) {
	itemsToDelete.add(item);
    }

    public void addItemsToCreate(ArrayList<MusicItem> items) {
	itemsToCreate.addAll(items);
    }

    public void addRelation(Tuple<MusicItem, MusicItem> relation) {
	relations.add(relation);
    }

    public void addRelations(MusicItem parent, List<MusicItem> children) {
	for (MusicItem child : children) {
	    if (child != null) {
		relations.add(new Tuple<MusicItem, MusicItem>(parent, child));
	    }
	}
    }

    public void addOldRelations(MusicItem parent, List<MusicItem> children) {
	for (MusicItem child : children) {
	    if (child != null) {
		oldRelations
			.add(new Tuple<MusicItem, MusicItem>(parent, child));
	    }
	}
    }

    public ArrayList<MusicItem> getItemsToCreate() {
	return itemsToCreate;
    }

    public ArrayList<Tuple<MusicItem, MusicItem>> getRelations() {
	return relations;
    }

    public ArrayList<Tuple<MusicItem, MusicItem>> getOldRelations() {
	return oldRelations;
    }

    public void removeData(MusicItem musicItem) {
	if (itemsToCreate.contains(musicItem)) {
	    itemsToCreate.remove(musicItem);
	}
	List<Integer> temp = new ArrayList<Integer>();
	for (Tuple<MusicItem, MusicItem> tuple : relations) {
	    if (tuple.getX().equals(musicItem)
		    || tuple.getY().equals(musicItem)) {
		temp.add((relations.lastIndexOf(tuple)));
	    }
	}

	for (Integer i : temp) {
	    relations.remove(i);
	}

    }

}
